﻿from odbc import atable as odbctable
from mysql import atable as mysqltable
from mysql import table
from mysql import relations

class object:
	#This is a manipultor for export object
	#beginning of class suite
	def __init__(self, name, fields, chars, clause=[], sql='', dsn='1sbdb05'):
		self.dsn=dsn
		self.name=name
		self.primary=['mysql_id','integer','not null','']
		self.fields=[self.primary]+fields
		self.sql=sql
		self.chars=chars
		self.comment='Копия таблицы 1С77 '+self.name
		self.clause=clause
		self.odbctable=odbctable()
		self.odbctable.table.base.dsn=self.dsn
		self.odbctable.name=self.name
		self.mysqltable=mysqltable()
		self.mysqltable.name='1s_'+self.name
		self.mysqltable.fields=self.fields
		self.mysqltable.primary=self.primary[0]
		self.mysqltable.comment=self.comment
	
	def columns(self):
		columns=[]
		for list in self.fields[1:]:
			columns+=[list[0]]
		return columns

	def charsindex(self):
		columns=self.columns()
		result=[]
		for item in range(len(columns)):
			if columns[item] in self.chars:
				result+=[item]
		return result

	def extract(self):
		'Function selects columns from the table where clause is satisfied or fetches sql'
		if self.sql=='':
			result=self.odbctable.select(self.columns(), self.clause)
		else:
			result==self.odbctable.table.base.fetch(self.sql)
		return result
		
	def transform(self, rows):
		'Function decode chars in rows'
		length=len(rows[0])
		charsindex=self.charsindex()
		result=[]
		for row in rows:
			temp=[]
			for x in range(length):
				if x in charsindex:
					temp.append(row[x].decode('cp1251'))
				else:
					temp.append(row[x])
			result.append(temp)
		return result
		
	def create(self):
		'Function creates the table in mysql'
		return self.mysqltable.create()
		
	def drop(self):
		'Function creates the table in mysql'
		return self.mysqltable.drop()
	
	def empty(self):
		'Function deletes from the table all records'
		return self.mysqltable.delete()
	
	def insert(self, rows):
		'Function inserts rows in mysql table'
		return self.mysqltable.insert(self.columns(),rows)
	
	def update(self):
		'Function updates table in mysql'
		return [self.empty(), self.insert(self.transform(self.extract()))]
		
	#end of class object suite

#Манипулятор реестра копируемых таблиц
#_1s_index=mysqltable()
#_1s_index.name='1s_index'
#_1s_index.fields=[['id','integer','not null',''],['_table','varchar(50)','',''],['_column','varchar(50)','',''],['_type','varchar(50)','','']]
#_1s_index.primary='id'
#_1s_index.comment='Список таблиц копируемых из 1С'


#def update_table(table):
#	table.drop('1s_'+item[0])
#	rows=table.select('1s_index',['_column', '_type'],[['_table','=',"'"+item[0]+"'"],['AND _type','NOT IN','('+"'"+'__ischar'+"'"+', '+"'"+'__sql'+"'"+', '+"'"+'__drop'+"'"+')']])
#	fields=[]
#	for row in rows:
#		field=[row[0],row[1],'','']
#		fields+=[field]
#	rows=table.select('1s_index',['_column'],[['_table','=',"'"+item[0]+"'"],['AND _type','=',"'"+'__ischar'+"'"]])
#	chars=[]
#	for row in rows:
#		char=row[0]
#		chars+=[char]
#	
#	object=object(item[0], )

#def update_list():
#	tables=table.select('1s_index', ['distinct _table'])
#	for item in tables:
#		update_table(item[0])


#Манипулятор для таблицы _1soper
_1soper=object('1soper', [['docid', 'varchar(9)','',''],['date','varchar(10)','',''],['time','varchar(6)','',''],['descr','varchar(50)','',''],['sum','decimal(16,2)','',''],['active','varchar(1)','','']],['descr'])